﻿// DNN 4.5.0 version

using System;
using System.Collections.Generic;
using System.Threading;
using System.Web;
using DotNetNuke.Common;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.UI;
using DotNetNuke.UI.WebControls;
using DotNetNuke.Web.DDRMenu.TemplateEngine;

namespace DotNetNuke.Web.DDRMenu
{
	internal static class DNNAbstract
	{
		public static string GetLoginUrl()
		{
			var request = HttpContext.Current.Request;
			var portalSettings = PortalController.GetCurrentPortalSettings();

			if (request.IsAuthenticated)
			{
				return Globals.NavigateURL(portalSettings.ActiveTab.TabID, "Logoff");
			}

			var returnUrl = HttpContext.Current.Request.RawUrl;
			if (returnUrl.IndexOf("?returnurl=") != -1)
			{
				returnUrl = returnUrl.Substring(0, returnUrl.IndexOf("?returnurl="));
			}
			returnUrl = HttpUtility.UrlEncode(returnUrl);

			if (portalSettings.LoginTabId != -1 && !String.IsNullOrEmpty(request.QueryString["override"]))
			{
				return Globals.NavigateURL(portalSettings.LoginTabId, "", "returnurl=" + returnUrl);
			}
			return Globals.NavigateURL(
				portalSettings.HomeTabId != -1 ? portalSettings.HomeTabId : portalSettings.ActiveTab.TabID,
				"Login",
				"returnurl=" + returnUrl);
		}

		public static string GetUserUrl()
		{
			var request = HttpContext.Current.Request;
			var portalSettings = PortalController.GetCurrentPortalSettings();

			if (!request.IsAuthenticated)
			{
				if (portalSettings.UserRegistration != (int)Globals.PortalRegistrationType.NoRegistration)
				{
					var returnUrl = HttpContext.Current.Request.RawUrl;
					if (returnUrl.IndexOf("?returnurl=") != -1)
					{
						returnUrl = returnUrl.Substring(0, returnUrl.IndexOf("?returnurl="));
					}
					returnUrl = HttpUtility.UrlEncode(returnUrl);

					return portalSettings.UserTabId != -1
					       	? Globals.NavigateURL(portalSettings.UserTabId, "", "returnurl=" + returnUrl)
					       	: (portalSettings.HomeTabId != -1
					       	   	? Globals.NavigateURL(portalSettings.HomeTabId, "Register", "returnurl=" + returnUrl)
					       	   	: Globals.NavigateURL(portalSettings.ActiveTab.TabID, "Register", "returnurl=" + returnUrl));
				}
			}
			else
			{
				return (portalSettings.UserTabId != -1)
				       	? Globals.NavigateURL(portalSettings.UserTabId)
				       	: Globals.NavigateURL(
				       		portalSettings.ActiveTab.TabID, "Profile", "UserID=" + UserController.GetCurrentUserInfo().UserID);
			}
			return "";
		}

		public static string GetCurrentCulture()
		{
			return Thread.CurrentThread.CurrentCulture.ToString();
		}

		public static IEnumerable<ITemplateProcessor> SupportedTemplateProcessors()
		{
			return new ITemplateProcessor[] {new XsltTemplateProcessor(), new TokenTemplateProcessor()};
		}

		public static int GetNavNodeOptions(bool includeHidden)
		{
			return (int)Navigation.NavNodeOptions.IncludeSiblings + (int)Navigation.NavNodeOptions.IncludeSelf;
		}

		public static bool IncludeHiddenSupported()
		{
			return false;
		}

		public static void DNNNodeToMenuNode(DNNNode dnnNode, MenuNode menuNode)
		{
		}

		public static bool RequestJQuery()
		{
			return false;
		}
	}
}